package cn.shop.merchant.controller.order;

import cn.dev33.satoken.stp.StpUtil;
import cn.shop.merchant.req.OrderQueryReq;
import cn.shop.merchant.req.OrderStatusReq;
import cn.shop.merchant.service.OrderService;
import cn.shop.utils.ApiResult;
import cn.shop.utils.annotation.OperationLog;
import cn.shop.utils.enums.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static cn.shop.utils.constant.Constant.UID;

/**
 * 订单管理
 *
 * @author : 李一涵
 * @date : Created in 2021-11-10 13:44
 * @description : 订单controller
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    /**
     * 按条件获取订单列表
     *
     * @param query 查询条件
     * @return 返回结果
     */
    @GetMapping("/list")
    public ApiResult<Map<String, Object>> list(OrderQueryReq query) {
        long uid = StpUtil.getSession().getLong(UID);
        if (uid == 0) {
            return ApiResult.fail("请检查登录状态");
        }
        /*if (Utils.isEmpty(query, "current", "size", "orderByTime", "status")) {
            LocalDateTime now = LocalDateTime.now();
            query.setStartTime(now.withHour(0).withMinute(0));
            query.setEndTime(now.withHour(23).withMinute(59));
        }*/
        Map<String, Object> data = orderService.orderList(query, uid);
        return ApiResult.success(data);
    }

    /**
     * 修改订单状态
     *
     * @param ids 编号
     * @param status 订单状态
     * @return 返回结果
     */
    @PutMapping("/change/status/{ids}")
    @OperationLog(desc = "变更订单状态", type = OperationType.UPDATE)
    public ApiResult<Map<String, Object>> changeStatus(@PathVariable("ids") long[] ids, @Validated @RequestBody OrderStatusReq status) {
        long uid = StpUtil.getSession().getLong(UID);
        if (uid == 0) {
            return ApiResult.fail("请检查登录状态");
        }
        boolean isSuccess = orderService.changeStatus(ids, uid, uid, status.getStatus());
        return ApiResult.result(isSuccess);
    }

    /**
     * 订单审核通过接口
     *
     * @param ids 编号
     * @return 返回结果
     */
    @PutMapping("/audit")
    @OperationLog(desc = "订单审核通过", type = OperationType.UPDATE)
    public ApiResult<Map<String, Object>> changeStatus(@RequestBody List<Long> ids) {
        long uid = StpUtil.getSession().getLong(UID);
        if (uid == 0) {
            return ApiResult.fail("请检查登录状态");
        }
        boolean isSuccess = orderService.audit(ids, uid, uid);
        return ApiResult.result(isSuccess);
    }

    /**
     * 查询订单总笔数和总金额
     *
     * @param query
     * @return
     */
    @GetMapping("/statistics")
    public ApiResult<Map<String, Object>> orderStatistics(OrderQueryReq query) {
        long uid = StpUtil.getSession().getLong(UID);
        /*if (Utils.isEmpty(query, "current", "size", "orderByTime", "status")) {
            LocalDateTime now = LocalDateTime.now();
            query.setStartTime(now.withHour(0).withMinute(0));
            query.setEndTime(now.withHour(23).withMinute(59));
        }*/
        Map<String, Object> data = orderService.statistics(query, uid);
        return ApiResult.success(data);
    }
}
